package com.example.wlt.personnel_manegement_system.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * Created by WLT on 2016/4/28.
 */
public class MemberFlowVpIndicator extends LinearLayout{

    private Paint mPaint;
    private Path mPath;
    private int mRectangleWidth;
    private int mRectangleHeight;
    private static final float RADIO_TRIANGLE_WIDTH = 1/3F; //比例
    private int mInitTranslationX;
    private int mTranslationX;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRectangleWidth = (int)(w / 2 * RADIO_TRIANGLE_WIDTH);
        mInitTranslationX = (int)(w / 2 / 2 - mRectangleWidth / 2);

        initTriangle();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(mInitTranslationX + mTranslationX,getHeight());
        canvas.drawPath(mPath,mPaint);
        canvas.restore();
        super.dispatchDraw(canvas);

    }

    //初始化三角形
    private void initTriangle() {
        mRectangleHeight = mRectangleWidth /20;
        mPath = new Path();
        mPath.moveTo(0,0);
        mPath.lineTo(mRectangleWidth,0);
        mPath.lineTo(mRectangleWidth,-mRectangleHeight);
        mPath.lineTo(0,-mRectangleHeight);
        mPath.close();
    }

    public void scroll(int position,float offset) {
        int tabWidth = getWidth() / 2;
        mTranslationX = (int) (tabWidth * offset + position * tabWidth);
        invalidate();
    }
    public MemberFlowVpIndicator(Context context) {
        this(context,null);
    }

    public MemberFlowVpIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化画笔
        mPaint  = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#3090e6"));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setPathEffect(new CornerPathEffect(3));
    }
}